package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC803 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * @return:
     */
    class Solution {
        public int[] hitBricks(int[][] grid, int[][] hits) {
            int R = grid.length, C = grid[0].length;
            int[] dr = {1, 0, -1, 0};
            int[] dc = {0, 1, 0, -1};

            int[][] A = new int[R][C];
            for (int r = 0; r < R; ++r)
                A[r] = grid[r].clone();
            for (int[] hit : hits)
                A[hit[0]][hit[1]] = 0;

            UF dsu = new UF(R * C + 1);
            for (int r = 0; r < R; ++r) {
                for (int c = 0; c < C; ++c) {
                    if (A[r][c] == 1) {
                        int i = r * C + c;
                        if (r == 0)
                            dsu.union(i, R * C);
                        if (r > 0 && A[r - 1][c] == 1)
                            dsu.union(i, (r - 1) * C + c);
                        if (c > 0 && A[r][c - 1] == 1)
                            dsu.union(i, r * C + c - 1);
                    }
                }
            }
            int t = hits.length;
            int[] ans = new int[t--];

            while (t >= 0) {
                int r = hits[t][0];
                int c = hits[t][1];
                int preRoof = dsu.top();
                if (grid[r][c] == 0) {
                    t--;
                } else {
                    int i = r * C + c;
                    for (int k = 0; k < 4; ++k) {
                        int nr = r + dr[k];
                        int nc = c + dc[k];
                        if (0 <= nr && nr < R && 0 <= nc && nc < C && A[nr][nc] == 1)
                            dsu.union(i, nr * C + nc);
                    }
                    if (r == 0)
                        dsu.union(i, R * C);
                    A[r][c] = 1;
                    ans[t--] = Math.max(0, dsu.top() - preRoof - 1);
                }
            }

            return ans;
        }
    }

    public class UF {
        // 连通分量个数
        private int count;
        // 存储一棵树
        private int[] parent;
        // 记录树的“重量”
        private int[] size;

        public UF(int n) {
            this.count = n;
            parent = new int[n];
            size = new int[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                size[i] = 1;
            }
        }

        /* 将 p 和 q 连接 */
        public void union(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            if (rootP == rootQ)
                return;

            // 小树接到大树下面，较平衡
            if (size[rootP] > size[rootQ]) {
                parent[rootQ] = rootP;
                size[rootP] += size[rootQ];
            } else {
                parent[rootP] = rootQ;
                size[rootQ] += size[rootP];
            }
            count--;
        }

        /* 判断 p 和 q 是否连通 */
        public boolean connected(int p, int q) {
            int rootP = find(p);
            int rootQ = find(q);
            return rootP == rootQ;
        }

        private int find(int x) {
            while (parent[x] != x) {
                // 进行路径压缩
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }

        /* 返回图中有多少个连通分量 */
        public int count() {
            return count;
        }

        public int size(int x) {
            return size[find(x)];
        }

        public int top() {
            return size(size.length - 1) - 1;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC803 an = new LC803();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
